//---------------------------------------------------------------------------------
// Copyright (c) 2011, Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//---------------------------------------------------------------------------------

namespace Microsoft.ServiceBus.Samples.SessionMessages
{
    using System;
    using System.Threading;
    using Microsoft.ServiceBus;
    using Microsoft.ServiceBus.Description;
    using Microsoft.ServiceBus.Messaging;

    class Program
    {
        #region Fields
        static string ServiceBusNamespace;
        static string ServiceBusIssuerName;
        static string ServiceBusIssuerKey;

        // Delay to simulate processing time
        static int senderDelay = 100;
        #endregion

        static void Main(string[] args)
        {
            ParseArgs(args);
            Console.Title = "MessageSender";

            // Send messages to queue which does not require session
            QueueClient queueClient = CreateQueueClient(SampleManager.SessionlessQueueName);
            Console.WriteLine("Preparing to send messages to {0}...", queueClient.Path);
            Thread.Sleep(3000);

            SendMessages(queueClient);

            // Send messages to queue requiring session
            queueClient = CreateQueueClient(SampleManager.SessionQueueName);
            Console.WriteLine("Preparing to send messages to {0}...", queueClient.Path);
            SendMessages(queueClient);

            // All messages sent
            Console.WriteLine("\nSender complete.");
            Console.ReadLine();
        }

        static void SendMessages(QueueClient queueClient)
        {
            // Send messages to queue:
            Console.WriteLine("Sending messages to queue {0}", queueClient.Path);

            System.Random rand = new Random();
            for (int i = 0; i < SampleManager.NumMessages; ++i)
            {
                string sessionName = rand.Next(SampleManager.NumSessions).ToString();
                BrokeredMessage message = CreateSessionMessage(sessionName);
                queueClient.Send(message);
                SampleManager.OutputMessageInfo("SEND: ", message);
                Thread.Sleep(senderDelay);
            }

            Console.WriteLine();
        }

        // Create the runtime entities (queue client)
        static QueueClient CreateQueueClient(string queueName)
        {
            Uri runtimeUri = ServiceBusEnvironment.CreateServiceUri("sb", ServiceBusNamespace, string.Empty);
            
            MessagingFactory messagingFactory = MessagingFactory.Create(
                runtimeUri,
                TokenProvider.CreateSharedSecretTokenProvider(ServiceBusIssuerName, ServiceBusIssuerKey));

            return messagingFactory.CreateQueueClient(queueName);
        }

        static BrokeredMessage CreateSessionMessage(string sessionId)
        {
            BrokeredMessage message = new BrokeredMessage();
            message.SessionId = sessionId;
            message.MessageId = "Order_" + Guid.NewGuid().ToString().Substring(0,5);
            return message;
        }

        static void ParseArgs(string[] args)
        {
            if (args.Length != 3)
            {
                throw new ArgumentException("Incorrect number of arguments. args = {0}", args.ToString());
            }

            ServiceBusNamespace = args[0];
            ServiceBusIssuerName = args[1];
            ServiceBusIssuerKey = args[2];
        }
    }
}
